package com.wayne.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wayne.blog.common.Ret;
import com.wayne.blog.entity.Category;
import com.wayne.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public Ret<Page<Category>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Category> page = categoryService.page(new Page<>(current, size),
                new LambdaQueryWrapper<Category>()
                        .orderByAsc(Category::getSort));
        return Ret.success(page);
    }

    @PostMapping
    public Ret<?> add(@RequestBody Category category) {
        categoryService.save(category);
        return Ret.success();
    }

    @PutMapping("/{id}")
    public Ret<?> update(@PathVariable Long id, @RequestBody Category category) {
        category.setId(id);
        categoryService.updateById(category);
        return Ret.success();
    }

    @DeleteMapping("/{id}")
    public Ret<?> delete(@PathVariable Long id) {
        categoryService.removeById(id);
        return Ret.success();
    }
} 